package edu.gdin.ecommerce.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.DicDetail;
import edu.gdin.ecommerce.model.DicType;
import edu.gdin.ecommerce.model.ParamDetail;
import edu.gdin.ecommerce.model.ParamType;
import edu.gdin.ecommerce.service.IDicService;
import edu.gdin.ecommerce.util.Pager;

@Controller("dicAction")
@Scope("prototype")
public class DicAction extends ActionSupport {

	private IDicService dicService;
	
	private String MessageReturn;
	private String name;
	private String typeDesc;
	private Integer currentPage;
	
	private Pager pager;
	
	
	private String dicTypeId;
	
	private String dicName;
	private String dicValue;
	
	
	private List<DicType> allTypes;
	
	
	
	public String getMessageReturn() {
		return MessageReturn;
	}

	public void setMessageReturn(String messageReturn) {
		MessageReturn = messageReturn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTypeDesc() {
		return typeDesc;
	}

	public void setTypeDesc(String typeDesc) {
		this.typeDesc = typeDesc;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public IDicService getDicService() {
		return dicService;
	}
	
	@Resource(name="dicService")
	public void setDicService(IDicService dicService) {
		this.dicService = dicService;
	}
	

	public String getDicTypeId() {
		return dicTypeId;
	}

	public void setDicTypeId(String dicTypeId) {
		this.dicTypeId = dicTypeId;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	
	
	public String getDicName() {
		return dicName;
	}

	public void setDicName(String dicName) {
		this.dicName = dicName;
	}

	public String getDicValue() {
		return dicValue;
	}

	public void setDicValue(String dicValue) {
		this.dicValue = dicValue;
	}

	
	public List<DicType> getAllTypes() {
		return allTypes;
	}

	public void setAllTypes(List<DicType> allTypes) {
		this.allTypes = allTypes;
	}

	/**
	 * 用于分页查找系统参数类型
	 */
	public String queryDicTypeAction(){
			
		
		String tName = name==null ? "" : name.trim();
		String tDesc = typeDesc==null ? "" : typeDesc.trim();
		
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = dicService.queryDicType(tName, tDesc, cPage);
		
		setPager(pager); 
		setName(tName);
		setTypeDesc(tDesc);
		
		return "typeList";
	}
	
	public String addDicTypeAction(){
		
		if(dicTypeId==null || !Pattern.matches("\\w+|_+", dicTypeId) || name==null || !Pattern.matches("\\w+|[\u4e00-\u9fa5]|_*", name) || typeDesc==null || !Pattern.matches("\\w+|[\u4e00-\u9fa5]+", name))
			return "typeAdd";
		
		
		DicType dType = new DicType();
		dType.setDicTypeId(dicTypeId);
		dType.setName(name);
		dType.setTypeDesc(typeDesc);
		
		boolean isSuccess = dicService.addDicType(dType);
		
		if(isSuccess){
			setMessageReturn("success");
			return this.queryDicTypeAction();
		}else{
			this.addFieldError("error", "已存在相同名字!");
			return "typeAdd";
		}
	}
	
	public String queryDicDetailAction(){
		
		
		String name = dicName == null ? "" : dicName.trim();
		String value = dicValue == null ? "" : dicValue.trim();
		
		DicDetail detail = new DicDetail();
		detail.setName(name);
		detail.setValue(value);
		
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = dicService.queryDicDetail(detail, cPage);
		
		setPager(pager);
		
		return "detailList";
	}
	
	
	public String queryDicDetailByTypeId(){
		
		Integer cPage = currentPage;
		
		cPage = (cPage==null)? 1 : (cPage< 1 ? 1:cPage);
		
		Pager pager = dicService.queryDicDetailByTypeId(dicTypeId, cPage);
		
		setPager(pager);
		
		return "detailList";
	}
	

	public String queryAllTypes(){
		List<DicType> allTypes = dicService.queryAllTypes();
		setAllTypes(allTypes);
		return "detailAdd";
	}
	
	
	public String addDicDetailAction(){

		
			String strP = "^(\\w+|[\u4e00-\u9fa5]+)*(\\w+|[\u4e00-\u9fa5]+)$";
			
			if(dicTypeId==null || dicName==null || dicValue==null || 
					!Pattern.matches(strP, dicName) || !Pattern.matches(strP, dicValue))
				return this.queryAllTypes();
		
			
			DicType dicType = dicService.queryDicTypeById(dicTypeId);
			
			DicDetail pd = new DicDetail();
			pd.setDicType(dicType);
			pd.setName(dicName);
			pd.setValue(dicValue);
			
			boolean isSuccess = dicService.addDicDetail(pd);
			
			if(isSuccess){
				setMessageReturn("success");
				return queryDicDetailAction();
			}else{
				setMessageReturn("error");
				return this.queryAllTypes();
			}
	}
}
